package com.platform.core.web.controller;

import cn.hutool.core.collection.CollUtil;
import com.platform.aspect.WebLog;
import com.platform.core.service.SysDictGroupService;
import com.platform.dto.DictGroupQo;
import com.querydsl.core.BooleanBuilder;
import com.platform.comm.web.results.DataJsonResult;
import com.platform.comm.web.results.JsonResult;
import com.platform.comm.web.results.PageListJsonResult;
import org.apache.logging.log4j.util.Strings;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Objects;

/**
 * Description: 字典组管理
 *
 * @author libin Created on 2019/7/17
 **/
@RestController
@RequestMapping("/management/dict/group")
public class SysDictGroupController extends BaseController {

    private final SysDictGroupService sysDictGroupService;

    public SysDictGroupController(SysDictGroupService sysDictGroupService) {
        this.sysDictGroupService = sysDictGroupService;
    }

    @PostMapping(name = "新增字典组", path = "create")
    @WebLog(theme = "新增[字典组]", moduleName = "字典组管理", type = WebLog.OpType.ADD, root = DictGroupQo.class)
    public JsonResult create(@RequestBody DictGroupQo dictGroupQo) {
        return createOrModifyWithResult(sysDictGroupService::create, dictGroupQo);
    }

    @PostMapping(name = "修改字典组", path = "modify")
    @WebLog(theme = "修改[字典组]", moduleName = "字典组管理", type = WebLog.OpType.MOD, root = DictGroupQo.class)
    public JsonResult modify(@RequestBody DictGroupQo dictGroupQo) {
        return createOrModifyWithResult(sysDictGroupService::modify, dictGroupQo);
    }

    @PostMapping(name = "删除字典组", path = "remove")
    @WebLog(theme = "删除[字典组]", moduleName = "字典组管理", type = WebLog.OpType.DEL, root = Map.class)
    public JsonResult remove(@RequestBody Map<String, String> map) {
        String id = Objects.toString(map.get("id"), Strings.EMPTY);
        return removeOrOtherByIds(sysDictGroupService::remove, id);
    }

    @PostMapping(name = "分页查询字典组", path = "search")
    public PageListJsonResult<DictGroupQo> search(@RequestBody Map<String, String> map) {
        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
        if (CollUtil.isNotEmpty(map)) {
            map.forEach(parameters::add);
        }
        return queryByPaging(sysDictGroupService::queryGroups, new BooleanBuilder(), getPageable(map), parameters);
    }

    @PostMapping(name = "检查字典组编码", path = "checkCodeExists")
    public DataJsonResult<Long> checkCodeExists(@RequestBody Map<String, String> map) {
        String id = Objects.toString(map.get("id"), Strings.EMPTY);
        String groupCode = Objects.toString(map.get("groupCode"), Strings.EMPTY);
        return query(sysDictGroupService::validCode, id, groupCode);
    }

    @PostMapping(name = "检查字典组名称", path = "checkNameExists")
    public DataJsonResult<Long> checkNameExists(@RequestBody Map<String, String> map) {
        String id = Objects.toString(map.get("id"), Strings.EMPTY);
        String groupName = Objects.toString(map.get("groupName"), Strings.EMPTY);
        return query(sysDictGroupService::validName, id, groupName);
    }
}
